<template>
  <el-dialog
    :title="!dataForm.id ? '新增组织架构' : '修改组织架构'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="名称" prop="org_name">
        <el-input v-model="dataForm.org_name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="上级组织" prop="parent_name">
        <el-popover
          ref="orgListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="orgList"
            :props="orgListTreeProps"
            node-key="id"
            ref="orgListTree"
            @current-change="orgListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parent_name" v-popover:orgListPopover :readonly="true" placeholder="点击选择上级组织" class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item label="数据域" prop="data_code">
        <el-input v-model="dataForm.data_code" placeholder="数据域"></el-input>
      </el-form-item>
      <el-form-item label="排序号" prop="order_num">
        <el-input-number v-model="dataForm.order_num" controls-position="right" :min="0" label="排序号"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          org_name: '',
          parent_id: '',
          parent_name: '',
          data_code: '',
          order_num: '0'
        },
        orgList: [],
        orgListTreeProps: {
          label: 'org_name',
          children: 'children'
        },
        dataRule: {
          org_name: [
            { required: true, message: '组织不能为空', trigger: 'blur' }
          ],
          parent_name: [
            { required: true, message: '上级组织不能为空', trigger: 'blur' }
          ],
          data_code: [
            { required: true, message: '数据域不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/sys/org/list'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.orgList = treeDataTranslate(data, 'id', 'parent_id')
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/org/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.org_name = data.org.org_name
                this.dataForm.parent_id = data.org.parent_id
                this.dataForm.data_code = data.org.data_code
                this.dataForm.order_num = data.org.order_num
                this.orgListTreeSetCurrentNode()
              }
            })
          } else {
            this.orgListTreeSetCurrentNode()
          }
        })
      },
      // 菜单树选中
      orgListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parent_id = data.id
        this.dataForm.parent_name = data.org_name
      },
      // 菜单树设置当前选中节点
      orgListTreeSetCurrentNode () {
        this.$refs.orgListTree.setCurrentKey(this.dataForm.parent_id)
        this.dataForm.parent_name = (this.$refs.orgListTree.getCurrentNode() || {})['org_name']
      },
      // 表单提交
      dataFormSubmit () {
        this.$confirm('此操作会同步对所属人员重新进行数据权限设置，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/sys/org/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData({
                  'id': this.dataForm.id || undefined,
                  'org_name': this.dataForm.org_name,
                  'parent_id': this.dataForm.parent_id,
                  'data_code': this.dataForm.data_code,
                  'order_num': this.dataForm.order_num
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              }).catch((error) => {
                this.$message.error('请求异常');
              })
            }
          })
        })
      }
    }
  }
</script>
